#include "setting.h"
#include "ui_setting.h"
#include <QPainter>
#include <QFont>
#include <QLabel>
#include <QWidget>
#include <QAction>
#include <QMenu>
#include <QToolTip>
#include <QMouseEvent>
#include <QStandardItemModel>
#include <QMessageBox>
#include <QPushButton>
#include <QProcess>
#include <QDialog>
#include<QSoundEffect>
#include <QMenuBar>
#include <cmath>
#include<QDebug>
#include<QTimer>
#include <QWebEngineView>
#include <QWebEngineSettings>

#include"huiqi.h"
#include"single.h"

QSoundEffect *effect=new QSoundEffect;
//声音头文件设置

int t=0;
single best[4],middle;
single d;

int p=1;
int i=0;
int nn=-1;
int castlinggg=0;
int difference=0;
int chessmode=1;
int maxscorew=-10000;
int maxscoreb=-10000;
int minscore=100000;
huiqi h[1000];
single smove[10];
single sreturn[10];

setting::~setting()
{
    delete ui;
}

void setting::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    double l=height()/29;
    //绘制边框
    //外框
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawRect(l,l,l*27,l*27);
    //内框
    painter.setPen(Qt::white);
    painter.setBrush(Qt::black);
    painter.drawRect(l*2.5,l*2.5,l*24,l*24);
    //画格子
    painter.setPen(Qt::white);
    painter.setBrush(Qt::gray);
    //棋盘格
    for(int i=0;i<8;i++)
    {
        for(int i1=0;i1<8;i1++)
        {
            if((i+i1)%2==1)
            painter.drawRect(l*(2.5+3*i),l*(2.5+3*i1),l*3,l*3);
        }
    }
    double a=l/9*5;
    char b[2],c[2];
    b[0]='8';
    b[1]=c[1]='\0';
    c[0]='a';
    char*b1=b;
    char*c1=c;
    painter.translate(l,l);
    for(char i=1;i<=8;i++)
    {
        painter.drawText(QPoint(a,a/4*3+l*3*i),tr(b1));
        painter.drawText(QPoint(l*3*i-l/9,height()-5*a),tr(c1));
        b[0]--;
        c[0]++;
    }
}

//将棋子的图片与实际的棋子相联系
QLabel *label[32];
QVector<QString> pic;
void setting::getPicName(){
    pic.append(":/pics/pic/WRook.png");
    pic.append(":/pics/pic/WKnigt.png");
    pic.append(":/pics/pic/WBishop.png");
    pic.append(":/pics/pic/WQueen.png");
    pic.append(":/pics/pic/WKing.png");
    pic.append(":/pics/pic/WPawn.png");
    pic.append(":/pics/pic/BRook.png");
    pic.append(":/pics/pic/BKnigt.png");
    pic.append(":/pics/pic/BBishop.png");
    pic.append(":/pics/pic/BQueen.png");
    pic.append(":/pics/pic/BKing.png");
    pic.append(":/pics/pic/BPawn.png");
}

setting::setting(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::setting)
{
    ui->setupUi(this);

    //设置对弈界面大小
    this->setGeometry(0,0,1500,850);

    //棋子序号与图片关联
    QString name;
    getPicName();
    for (int i=0;i<32;i++){
        myChess[i].startChess(i);
        //白棋
        if (i<=4)
            name=pic.at(i);
        else
            if (i>=5&&i<=7)
            name=pic.at(7-i);
        else
            if (i>=8&&i<=15)
            name=pic.at(5);
        //黑棋
        else
            if (i>=16&&i<=20)
            name=pic.at(i-10);
        else
            if (i>=21&&i<=23)
            name=pic.at(29-i);
        else
            if (i>=24)
            name=pic.at(11);
//棋子在棋盘上的位置
        label[i]=new QLabel(this);
        label[i]->resize(height()/29*3,height()/29*3);
        label[i]->setPixmap(QPixmap(name));
        label[i]->setFixedSize(height()/29*2.5,height()/29*3);
        label[i]->setScaledContents(true);
        label[i]->setGeometry((myChess[i].x)*height()/29*3+height()/29*2.8,(myChess[i].y)*height()/29*3+height()/29*2.4,height()/29*3,height()/29*3);
        update();
    }
}

//判断该格中棋子类型
int setting::getNum(int xp, int yp){
    for (int i=0;i<32;i++)
        if (myChess[i].x==xp&&myChess[i].y==yp)
            return i;
    return -1;
}

//判断当前格子中是否有棋子
bool setting::exist(int xp, int yp){
    for (int i=0;i<32;i++)
        if (myChess[i].x==xp&&myChess[i].y==yp)
            return true;
    return false;
}

bool setting::middleChess(int n, int xp, int yp)
{
    //同列
    if (myChess[n].x==xp){
        if (yp>myChess[n].y){
            for (int i=myChess[n].y+1;i<yp;i++)
                if (exist(xp,i))
                    return true;
        }
        else {
            for (int i=yp+1;i<myChess[n].y;i++)
                if (exist(xp,i))
                    return true;
        }
    }
    //同行
    else if (myChess[n].y==yp)
    {
        if (xp>myChess[n].x){
            for (int i=myChess[n].x+1;i<xp;i++)
                if (exist(i,yp))
                    return true;
        }
        else {
            for (int i=xp+1;i<myChess[n].x;i++)
                if (exist(i,yp))
                    return true;
        }
    }
    //不同行也不同列
    else
    {
        if (xp>myChess[n].x){
            if (yp>myChess[n].y){
                int j=myChess[n].y+1;
                for (int i=myChess[n].x+1;i<xp;i++)
                    if (exist(i,j))
                        return true;
                    else
                        j++;
            }
            else{
                int j=myChess[n].y-1;
                for (int i=myChess[n].x+1;i<xp;i++)
                    if (exist(i,j))
                        return true;
                    else
                        j--;
            }
        }
        else{
            if (yp>myChess[n].y){
                int j=myChess[n].y+1;
                for (int i=myChess[n].x-1;i>xp;i--)
                    if (exist(i,j))
                        return true;
                    else
                        j++;
            }
            else{
                int j=myChess[n].y-1;
                for (int i=myChess[n].x-1;i>xp;i--)
                    if (exist(i,j))
                        return true;
                    else
                        j--;
            }
        }
    }
    return false;
}

//判断棋子颜色是否相同
bool setting::sameColor(int n1, int n2){
    if (myChess[n1].white==myChess[n2].white)
        return true;
    return false;
}

//走动是否合法（车）
bool setting::moveRook(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (exist(xp,yp)&&sameColor(n1,n2))
        return false;
    if (middleChess(n1,xp,yp))
        return false;
    if (xp!=myChess[n1].x&&yp!=myChess[n1].y)
        return false;
    return true;
}

//走动是否合法（马）
bool setting::moveKnight(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (exist(xp,yp)&&sameColor(n1,n2))
        return false;
    if (abs(xp-myChess[n1].x)==1&&abs(yp-myChess[n1].y)==2)
        return true;
    else if (abs(xp-myChess[n1].x)==2&&abs(yp-myChess[n1].y)==1)
        return true;
    return false;
}

//走动是否合法（象）
bool setting::moveBishop(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (exist(xp,yp)&&sameColor(n1,n2))
        return false;
    if (middleChess(n1,xp,yp))
        return false;
    if (abs(xp-myChess[n1].x)==abs(yp-myChess[n1].y))
        return true;
    return false;
}

//后走动是否合法
bool setting::moveQueen(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (exist(xp,yp)&&sameColor(n1,n2))
        return false;
    if (middleChess(n1,xp,yp))
        return false;
    if (xp==myChess[n1].x||yp==myChess[n1].y)
        return true;
    else if (abs(xp-myChess[n1].x)==abs(yp-myChess[n1].y))
        return true;
    return false;
}


//bool whiteCastled=false,blackCastled=false;

bool setting::castling(int n, int xp,int yp)
{
    //白方易位
    if (myChess[n].white)
    {
        //短易位
        if (xp>myChess[n].x)
        {
            if (!myChess[7].moved&&!myChess[n].moved)//王和车都没有移动过
            {
                if (!middleChess(n,myChess[7].x,myChess[7].y))//王车之间没有其他棋子
               {
                    for (int i=16;i<32;i++)
                   {
                        //检测途中的三个格子是否可能有王被将军的情况
                        if (threat(i,myChess[n].x,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x+1,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x+2,myChess[n].y))
                            return false;
                    }
                }
                else
                    return false;
            }
            else
                return false;
        }
        else{
            //长易位
            if (!myChess[0].moved&&!myChess[n].moved)
            {
                if (!middleChess(n,myChess[0].x,myChess[0].y))
                {
                    for (int i=16;i<32;i++)
                    {
                        if (threat(i,myChess[n].x,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x-1,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x-2,myChess[n].y))
                            return false;
                    }
                }
                else
                    return false;
            }
            else
                return false;
        }
    }
    //黑方易位
    else{
        if (xp>myChess[n].x)
        {
            //短易位
            if (!myChess[23].moved&&!myChess[n].moved)
            {
                if (!middleChess(n,myChess[23].x,myChess[23].y))
                {
                    for (int i=0;i<16;i++){
                        if (threat(i,myChess[n].x,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x+1,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x+2,myChess[n].y))
                            return false;
                    }
                }else
                    return false;
            }else
                return false;
        }
        else{
            //长易位
            if (!myChess[16].moved&&!myChess[n].moved)
            {
                if (!middleChess(n,myChess[16].x,myChess[16].y))
                {
                    for (int i=0;i<16;i++){
                        if (threat(i,myChess[n].x,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x-1,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x-2,myChess[n].y))
                            return false;
                    }
                }else
                    return false;
            }else
                return false;
        }
    }
    return true;
}

//王走动是否合法
bool setting::moveKing(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (exist(xp,yp)&&sameColor(n1,n2))
        return false;
    //斜走
    if (abs(xp-myChess[n1].x)==1&&abs(yp-myChess[n1].y)==1)
        return true;
    //竖走
    if (xp==myChess[n1].x&&abs(yp-myChess[n1].y)==1)
        return true;
    //横走
    if (abs(xp-myChess[n1].x)==1&&yp==myChess[n1].y)
        return true;
    //易位
    if (yp==myChess[n1].y&&abs(xp-myChess[n1].x)==2)
    {
        if (myChess[n1].white)
            return castling(n1,xp,yp);
        else if (!myChess[n1].white)
            return castling(n1,xp,yp);
        else
            return false;
    }
    return false;
}

int white=16,black=16;

//吃过路兵
bool setting::enPassant(int n1,int xp, int yp)
{
    int n2=-1;
    if (myChess[n1].white)
    {
        n2=getNum(xp,yp+1);
        if(n2==nn)
        {
            //白方
            if (n2>=24&&n2<32)
            {
                if (myChess[n2].y==3&&myChess[n2].moved==1)
                {
                    myChess[n2].dead=true;
                    myChess[n2].x=-1;
                    myChess[n2].y=-1;
                    label[n2]->clear();
                    black--;
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }
        return false;
    }
    else
    {
        n2=getNum(xp,yp-1);
        if(n2==nn)
        {
            if (n2>=8&&n2<16)
            {
                if (myChess[n2].y==4&&myChess[n2].moved==1)
                {
                    myChess[n2].dead=true;
                    myChess[n2].x=-1;
                    myChess[n2].y=-1;
                    label[n2]->clear();
                    white--;
                    return true;
                }
                else
                    return false;
            }
        else
            return false;
        }
        return false;
    }
}

//兵的移动
bool setting::movePawn(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (n2!=-1&&sameColor(n1,n2))
        return false;
    if (n2!=-1&&myChess[n1].x==xp&&abs(myChess[n1].y-yp)==1)
        return false;
    if (xp==myChess[n1].x){
        if (myChess[n1].white){
            if ((myChess[n1].y-yp)==2&&!myChess[n1].moved)
                return true;
            else if (myChess[n1].y-yp==1)
                return true;
        }
        else {
            if ((yp-myChess[n1].y)==2&&!myChess[n1].moved)
                return true;
            else if (yp-myChess[n1].y==1)
                return true;
        }
    }
    else if (abs(xp-myChess[n1].x)==1){
        if (myChess[n1].white&&(myChess[n1].y-yp==1)){
            if (n2!=-1)
                return true;
            else if (enPassant(n1,xp,yp))
                return true;
        }
        else if (!myChess[n1].white&&(yp-myChess[n1].y==1)){
            if (n2!=-1)
                return true;
            else if (enPassant(n1,xp,yp))
                return true;
        }
    }
    return false;
}

bool setting::threat(int n, int xk, int yk)
{
    if (myChess[n].dead)
        return false;
    if (myChess[n].chessType==0)
    {//车
        if (myChess[n].x==xk||myChess[n].y==yk){
            if (middleChess(n,xk,yk))
                return false;
            else
                return true;
         }
         else
            return false;
    }
    else if (myChess[n].chessType==1)
    { //马
        if (abs(xk-myChess[n].x)==1&&abs(yk-myChess[n].y)==2)
            return true;
        else if (abs(xk-myChess[n].x)==2&&abs(yk-myChess[n].y)==1)
            return true;
        else
            return false;
    }
    else if (myChess[n].chessType==2)
    { //象
        if (abs(xk-myChess[n].x)==abs(yk-myChess[n].y)){
            if (middleChess(n,xk,yk))
                return false;
            else
                return true;
        }
        else
            return false;
    }
    else if (myChess[n].chessType==3)
    { //后
        if (myChess[n].x==xk||myChess[n].y==yk){
            if (middleChess(n,xk,yk))
                return false;
            else
                return true;
        }
        else if (abs(xk-myChess[n].x)==abs(yk-myChess[n].y)){
            if (middleChess(n,xk,yk))
                return false;
            else
                return true;
        }
        else
            return false;
    }
    else if (myChess[n].chessType==4)
    { //王
        if (abs(myChess[n].x-xk)==1&&abs(myChess[n].y-yk)==1)
            return true;
        else if (myChess[n].x==xk&&abs(myChess[n].y-yk)==1)
            return true;
        else if (abs(myChess[n].x-xk)==1&&myChess[n].y==yk)
            return true;
        else
            return false;
    }
    else if (myChess[n].chessType==5)
    { //兵
        if (myChess[n].white){
            if ((myChess[n].y-yk)==1&&abs(myChess[n].x-xk)==1)
                return true;
            else
                return false;
        }
        else{
            if ((yk-myChess[n].y)==1&&abs(xk-myChess[n].x)==1)
                return true;
            else
                return false;
        }
    }
    return false;
}

int whiteTurn=1;
bool isthreatened=false;

//判断是否有棋可走
bool setting::legalMove(){
    int os=0,es=0,oe=0,ee=0;
    if (whiteTurn==1){
        os=0;
        oe=16;
        es=16;
        ee=32;
    }
    else{
        os=16;
        oe=32;
        es=0;
        ee=16;
    }
    for (int i=os;i<oe;i++){
        int xt=myChess[i].x;
        int yt=myChess[i].y;
        int nt=-1,j=0,m=0;
        if (myChess[i].chessType==stone::rook)
        {
            for (;j<8;j++){
                nt=getNum(j,yt);
                if (move(i,nt,j,yt))
                    return true;
            }
            for (;m<8;m++){
                nt=getNum(xt,m);
                if (move(i,nt,xt,m))
                    return true;
            }
        }
        else if(myChess[i].chessType==stone::knight){
            int xx[8]={xt-1,xt-1,xt-2,xt-2,xt+1,xt+1,xt+2,xt+2};
            int yy[8]={yt-2,yt+2,yt-1,yt+1,yt-2,yt+2,yt-1,yt+1};
            for (j=0;j<8;j++)
            {
                nt=getNum(xx[j],yy[j]);
                if (move(i,nt,xx[j],yy[j]))
                return true;
            }
        }
        else if (myChess[i].chessType==stone::bishop){
            for (j=xt+1;j<8;j++){
                m=yt+j-xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt+1;j<8;j++){
                m=yt-j+xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt-1;j>=0;j--){
                m=yt+j-xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt-1;j>=8;j--){
                m=yt-j+xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
        }
        else if (myChess[i].chessType==stone::queen){
            //横
            for (j=0;j<8;j++){
                nt=getNum(j,yt);
                if (move(i,nt,j,yt))
                    return true;
            }
            //竖
            for (m=0;m<8;m++){
                nt=getNum(xt,m);
                if (move(i,nt,xt,m))
                    return true;
            }
            //斜线
            for (j=xt+1;j<8;j++){
                m=yt+j-xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt+1;j<8;j++){
                m=yt-j+xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt-1;j>=0;j--){
                m=yt+j-xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt-1;j>=8;j--){
                m=yt-j+xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
        }
        else if (myChess[i].chessType==stone::king)
        {
            p=1;
            //qDebug() << "测试王";
            int xx[8]={xt-1,xt,xt+1,xt+1,xt+1,xt,xt-1,xt-1};
            int yy[8]={yt-1,yt-1,yt-1,yt,yt+1,yt+1,yt+1,yt};
            for (j=0;j<8;j++){
                nt=getNum(xx[j],yy[j]);
                if(xx[j]>=0&&xx[j]<=7&&yy[j]>=0&&yy[j]<=7)
                {
                  if (move(i,nt,xx[j],yy[j]))
                  {
                     p=0;
                     //qDebug() << "可移动" << endl;
                     return true;
                  }
                }
            }
        }
        else if (myChess[i].chessType==stone::pawn){
            if (whiteTurn==1)
                m=yt-1;
            else
                m=yt+1;
            nt=getNum(xt,m);
            if (move(i,nt,xt,m))
                return true;
            j=xt-1;
            if (move(i,nt,j,m))
                return true;
            j=xt+1;
            if (move(i,nt,j,m))
                return true;
        }
    }
    return false;
}

single setting::aiMove()
{
    whiteTurn*=-1;
    //qDebug()<<"ai"<<t<<endl;
    int os=0,es=0,oe=0,ee=0;
    if (t%2==0){
        os=0;
        oe=16;
        es=16;
        ee=32;
    }
    else{
        os=16;
        oe=32;
        es=0;
        ee=16;
    }
    for (int i=oe-1;i>=os;i--)//从兵开始遍历，计数棋步价值，并作出相应判断
    {
        if(!myChess[i].dead)
        {int xt=myChess[i].x;
        int yt=myChess[i].y;
        int nt=-1,j=0,m=0;
        if (myChess[i].chessType==stone::rook)
               {
                   for (;j<8;j++)
                   {
                       nt=getNum(j,yt);
                       if (move(i,nt,j,yt))
                       {
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=j;
                           myChess[i].y=yt;
                           if (nt!=-1)
                               myChess[nt].dead=true;
                                  if(t==1)
                               {
                                      if(best[t-1].n1==-1)
                                      {
                                          best[t-1].s(i,nt,j,yt,sumvalue(myChess[i].white));
                                      }
                                      else
                                      {
                                          if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                          {
                                               best[t-1].s(i,nt,j,yt,sumvalue(myChess[i].white));
                                          }
                                      }

                               }
                                   else
                               {
                                   t--;
                                   aiMove();
                                   t++;
                                   if(best[t-1].n1==-1)
                                   {
                                       best[t-1].s(i,nt,j,yt,(-best[t-2].bestvalue));
                                   }
                                   else
                                   {
                                       if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                       {
                                           best[t-1].s(i,nt,j,yt,(-best[t-2].bestvalue));
                                       }
                                   }
                                }
                           //还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
                   for (;m<8;m++){
                       nt=getNum(xt,m);
                       if (move(i,nt,xt,m))
                       {
                           //假定走棋
                           int xt=myChess[i].x,yt=myChess[i].y;
                           myChess[i].x=j;
                           myChess[i].y=yt;
                           if (nt!=-1)
                               myChess[nt].dead=true;

                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,xt,m,sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,xt,m,sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,xt,m,(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,xt,m,(-best[t-2].bestvalue));
                                }
                            }
                         }


                           //还原
                           myChess[i].x=xt;
                           myChess[i].y=yt;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
               }
               else if(myChess[i].chessType==stone::knight){
                   int xx[8]={xt-1,xt-1,xt-2,xt-2,xt+1,xt+1,xt+2,xt+2};
                   int yy[8]={yt-2,yt+2,yt-1,yt+1,yt-2,yt+2,yt-1,yt+1};
                   for (j=0;j<8;j++){
                       nt=getNum(xx[j],yy[j]);
                       if (move(i,nt,xx[j],yy[j]))
                       {
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=xx[j];
                           myChess[i].y=yy[j];
                           if (nt!=-1)
                               myChess[nt].dead=true;
                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,xx[j],yy[j],sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,xx[j],yy[j],sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,xx[j],yy[j],(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,xx[j],yy[j],(-best[t-2].bestvalue));
                                }
                            }
                         }
                           //还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
               }
               else if (myChess[i].chessType==stone::bishop){
                   for (j=xt+1;j<8;j++){
                       m=yt+j-xt;
                       nt=getNum(j,m);
                       if (move(i,nt,j,m))
                       {
                           //qDebug()<<"bbbbbbb"<<endl;
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=j;
                           myChess[i].y=m;
                           if (nt!=-1)
                               myChess[nt].dead=true;
                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                                }
                            }
                         }
                           //还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
                   for (j=xt+1;j<8;j++){
                       m=yt-j+xt;
                       nt=getNum(j,m);
                       if (move(i,nt,j,m))
                       {
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=j;
                           myChess[i].y=m;
                           if (nt!=-1)
                               myChess[nt].dead=true;
                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                                }
                            }
                         }
                           //还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
                   for (j=xt-1;j>=0;j--){
                       m=yt+j-xt;
                       nt=getNum(j,m);
                       if (move(i,nt,j,m))
                       {
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=j;
                           myChess[i].y=m;
                           if (nt!=-1)
                               myChess[nt].dead=true;
                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                                }
                            }
                         }
                           //还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
                   for (j=xt-1;j>=8;j--){
                       m=yt-j+xt;
                       nt=getNum(j,m);
                       if (move(i,nt,j,m))
                       {
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=j;
                           myChess[i].y=m;
                           if (nt!=-1)
                               myChess[nt].dead=true;
                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                                }
                            }
                         }
                           //还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
               }
               else if (myChess[i].chessType==stone::queen){
                   //横
                   for (j=0;j<8;j++){
                       nt=getNum(j,yt);
                       if (move(i,nt,j,yt))
                       {
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=j;
                           myChess[i].y=yt;
                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,j,yt,sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,j,yt,sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,j,yt,(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,j,yt,(-best[t-2].bestvalue));
                                }
                            }
                         }
                           //还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
                   //竖
                   for (m=0;m<8;m++){
                       nt=getNum(xt,m);
                       if (move(i,nt,xt,m))
                       {
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=xt;
                           myChess[i].y=m;
                           if (nt!=-1)
                               myChess[nt].dead=true;
                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,j,xt,sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,j,xt,sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,j,xt,(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,j,xt,(-best[t-2].bestvalue));
                                }
                            }
                         }
                           //还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
                   //斜线
                   for (j=xt+1;j<8;j++){
                       m=yt+j-xt;
                       nt=getNum(j,m);
                       if (move(i,nt,j,m))
                       {
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=j;
                           myChess[i].y=m;
                           if (nt!=-1)
                               myChess[nt].dead=true;
                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                                }
                            }
                         }//还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
                   for (j=xt+1;j<8;j++){
                       m=yt-j+xt;
                       nt=getNum(j,m);
                       if (move(i,nt,j,m))
                       {
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=j;
                           myChess[i].y=m;
                           if (nt!=-1)
                               myChess[nt].dead=true;
                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                                }
                            }
                         }
                           //还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
                   for (j=xt-1;j>=0;j--){
                       m=yt+j-xt;
                       nt=getNum(j,m);
                       if (move(i,nt,j,m))
                       {
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=j;
                           myChess[i].y=m;
                           if (nt!=-1)
                               myChess[nt].dead=true;
                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                                }
                            }
                         }
                           //还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
                   for (j=xt-1;j>=8;j--){
                       m=yt-j+xt;
                       nt=getNum(j,m);
                       if (move(i,nt,j,m))
                       {
                           //假定走棋
                           int x=myChess[i].x,y=myChess[i].y;
                           myChess[i].x=j;
                           myChess[i].y=m;
                           if (nt!=-1)
                               myChess[nt].dead=true;
                           if(t==1)
                        {
                               if(best[t-1].n1==-1)
                               {
                                   best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                               }
                               else
                               {
                                   if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                   {
                                        best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                                   }
                               }

                        }
                            else
                        {
                            t--;
                            aiMove();
                            t++;
                            if(best[t-1].n1==-1)
                            {
                                best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                            }
                            else
                            {
                                if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                {
                                    best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                                }
                            }
                         }
                           //还原
                           myChess[i].x=x;
                           myChess[i].y=y;
                           if (nt!=-1)
                               myChess[nt].dead=false;
                       }
                   }
               }
               else if (myChess[i].chessType==stone::king)
               {
                   p=1;
                   //qDebug() << "测试王" << endl;
                   int xx[8]={xt-1,xt,xt+1,xt+1,xt+1,xt,xt-1,xt-1};
                   int yy[8]={yt-1,yt-1,yt-1,yt,yt+1,yt+1,yt+1,yt};
                   for (j=0;j<8;j++){
                       nt=getNum(xx[j],yy[j]);
                       if(xx[j]>=0&&xx[j]<=7&&yy[j]>=0&&yy[j]<=7)
                       {
                         if (move(i,nt,xx[j],yy[j]))
                         {
                            p=0;
                            //qDebug() << "可移动" << endl;
                            //假定走棋
                            int x=myChess[i].x,y=myChess[i].y;
                            myChess[i].x=xx[j];
                            myChess[i].y=yy[j];
                            if (nt!=-1)
                                myChess[nt].dead=true;
                            if(t==1)
                         {
                                if(best[t-1].n1==-1)
                                {
                                    best[t-1].s(i,nt,xx[j],yy[j],sumvalue(myChess[i].white));
                                }
                                else
                                {
                                    if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                                    {
                                         best[t-1].s(i,nt,xx[j],yy[j],sumvalue(myChess[i].white));
                                    }
                                }

                         }
                             else
                         {
                             t--;
                             aiMove();
                             t++;
                             if(best[t-1].n1==-1)
                             {
                                 best[t-1].s(i,nt,xx[j],yy[j],(-best[t-2].bestvalue));
                             }
                             else
                             {
                                 if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                                 {
                                     best[t-1].s(i,nt,xx[j],yy[j],(-best[t-2].bestvalue));
                                 }
                             }
                          }
                            //还原
                            myChess[i].x=x;
                            myChess[i].y=y;
                            if (nt!=-1)
                                myChess[nt].dead=false;
                         }
                       }
                   }
               }
               else if (myChess[i].chessType==stone::pawn){
                   if (whiteTurn==1)
                       m=yt-1;
                   else
                       m=yt+1;
                   nt=getNum(xt,m);
                   if (move(i,nt,xt,m))
                   {
                       //假定走棋
                       int x=myChess[i].x,y=myChess[i].y;
                       myChess[i].x=xt;
                       myChess[i].y=m;
                       if (nt!=-1)
                           myChess[nt].dead=true;
                       if(t==1)
                    {
                           if(best[t-1].n1==-1)
                           {
                               best[t-1].s(i,nt,xt,m,sumvalue(myChess[i].white));
                           }
                           else
                           {
                               if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                               {
                                    best[t-1].s(i,nt,xt,m,sumvalue(myChess[i].white));
                               }
                           }

                    }
                        else
                    {
                        t--;
                        aiMove();
                        t++;
                        if(best[t-1].n1==-1)
                        {
                            best[t-1].s(i,nt,xt,m,(-best[t-2].bestvalue));
                        }
                        else
                        {
                            if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                            {
                                best[t-1].s(i,nt,xt,m,(-best[t-2].bestvalue));
                            }
                        }
                     }
                       //还原
                       myChess[i].x=x;
                       myChess[i].y=y;
                       if (nt!=-1)
                           myChess[nt].dead=false;
                   }
                   j=xt-1;
                   if (move(i,nt,j,m))
                   {
                       //假定走棋
                       int x=myChess[i].x,y=myChess[i].y;
                       myChess[i].x=j;
                       myChess[i].y=m;
                       if (nt!=-1)
                           myChess[nt].dead=true;
                       if(t==1)
                    {
                           if(best[t-1].n1==-1)
                           {
                               best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                           }
                           else
                           {
                               if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                               {
                                    best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                               }
                           }

                    }
                        else
                    {
                        t--;
                        aiMove();
                        t++;
                        if(best[t-1].n1==-1)
                        {
                            best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                        }
                        else
                        {
                            if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                            {
                                best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                            }
                        }
                     }

                       //还原
                       myChess[i].x=x;
                       myChess[i].y=y;
                       if (nt!=-1)
                           myChess[nt].dead=false;
                   }
                   j=xt+1;
                   if (move(i,nt,j,m))
                   {
                       //假定走棋
                       int x=myChess[i].x,y=myChess[i].y;
                       myChess[i].x=j;
                       myChess[i].y=m;
                       if (nt!=-1)
                           myChess[nt].dead=true;
                       if(t==1)
                    {
                           if(best[t-1].n1==-1)
                           {
                               best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                           }
                           else
                           {
                               if(sumvalue(myChess[i].white)>best[t-1].bestvalue)
                               {
                                    best[t-1].s(i,nt,j,m,sumvalue(myChess[i].white));
                               }
                           }

                    }
                        else
                    {
                        t--;
                        aiMove();
                        t++;
                        if(best[t-1].n1==-1)
                        {
                            best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                        }
                        else
                        {
                            if((-best[t-2].bestvalue)<best[t-1].bestvalue)
                            {
                                best[t-1].s(i,nt,j,m,(-best[t-2].bestvalue));
                            }
                        }
                     }

                       //还原
                       myChess[i].x=x;
                       myChess[i].y=y;
                       if (nt!=-1)
                           myChess[nt].dead=false;
                   }
               }
           }
    }
    whiteTurn*=-1;
    return best[t-1];
}

//判断将杀
bool setting::win()
{
    if (whiteTurn==1){
        for (int i=16;i<32;i++)
        {
            if (threat(i,myChess[4].x,myChess[4].y))
            {
                isthreatened=true;
                break;
            }
            else
                isthreatened=false;
        }
        if (isthreatened)
        {

            if (!legalMove())
            {
                return true;}
            else
                return false;
        }
        else
            return false;
    }
    else{
        for (int i=0;i<16;i++){
            if (threat(i,myChess[20].x,myChess[20].y))
            {
                isthreatened=true;
                break;
            }
            else
                isthreatened=false;
        }
        if (isthreatened)
        {
            if (!legalMove())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
            return false;
    }
}
//走棋方无子可动
bool setting::bihe(){
    if (whiteTurn==1){
        for (int i=16;i<32;i++){
            if (threat(i,myChess[4].x,myChess[4].y)){
                isthreatened=true;
                break;
            }
            else
                isthreatened=false;
        }
        if (!isthreatened){
            if (!legalMove())
                return true;
            else
                return false;
        }
        else
            return false;
    }
    else{
        for (int i=0;i<16;i++){
            if (threat(i,myChess[20].x,myChess[20].y)){
                isthreatened=true;
                break;
            }
            else
                isthreatened=false;
        }
        if (!isthreatened){
            if (!legalMove())
                return true;
            else
                return false;
        }
        else
            return false;
    }
}
//双方都没有获胜能力
bool setting::heqi(){
    if (white==1&&black==2)
    {
        if(!myChess[21].dead)
            return true;
        if(!myChess[22].dead)
            return true;
        if(!myChess[18].dead)
            return true;
        if(!myChess[17].dead)
            return true;
    }
    if (white==2&&black==1)
    {
        if(!myChess[1].dead)
            return true;
        if(!myChess[2].dead)
            return true;
        if(!myChess[5].dead)
            return true;
        if(!myChess[6].dead)
            return true;
    }
    if (white==1&&black==1)
        return true;
    return false;
}


//该移动是否合法
bool setting::move(int n1, int n2, int xp, int yp){
    //是否轮到该方走棋
    if (n1>=0&&n1<=15){
        if (whiteTurn==1) ;
        else
            return false;
    }
    else if (n1>=16&&n1<=31){
        if (whiteTurn==1)
            return false;
    }
    else
        return false;
    if (xp<0||xp>7||yp<0||yp>7)
        return false;

    //是否符合规则
    switch(myChess[n1].chessType){
        case 0:
            if(moveRook(n1,n2,xp,yp));
            else return false;
            break;
        case 1:
            if(moveKnight(n1,n2,xp,yp));
            else return false;
            break;
        case 2:
            if(moveBishop(n1,n2,xp,yp));
            else return false;
            break;
        case 3:
            if(moveQueen(n1,n2,xp,yp));
            else return false;
            break;
        case 4:
            if(moveKing(n1,n2,xp,yp));
            else return false;
            break;
        case 5:
            if(movePawn(n1,n2,xp,yp));
            else return false;
            break;
        default:
            return false;
            break;
    }

    //如果移动
    int xt=myChess[n1].x,yt=myChess[n1].y;
    myChess[n1].x=xp;
    myChess[n1].y=yp;
    if (n2!=-1)
        myChess[n2].dead=true;
    if (myChess[n1].white){
        //白方被将军
        for (int i=16;i<32;i++){
            if (!myChess[i].dead&&threat(i,myChess[4].x,myChess[4].y)){
                isthreatened=true;
                break;
            }
            else isthreatened=false;
        }
    }
    else {
        //黑方被将军
        for (int i=0;i<16;i++){
            if (!myChess[i].dead&&threat(i,myChess[20].x,myChess[20].y)){
                isthreatened=true;
                break;
            }
            else isthreatened=false;
        }
    }
    //还原棋子
    myChess[n1].x=xt;
    myChess[n1].y=yt;
    if (n2!=-1)
        myChess[n2].dead=false;
    if (isthreatened)
        return false;
    else
        return true;
}

//悔棋
void setting::hui(int n1,int n2,int x1,int x2,int y1,int y2)
{
    //执子方改变
    whiteTurn*=-1;
    QFont font ( 0, 20, 75);
    ui->label1->move(890,100);
    if(whiteTurn==1)
        ui->label1->setText("白棋执子");
    else
        ui->label1->setText("黑棋执子");
    ui-> label1->setFont(font);
    ui->label2->move(890,300);
    //恢复被吃掉的棋子
    if (n2!=-1)
    {
       myChess[n2].dead=false;
       myChess[n2].x=x2;
       myChess[n2].y=y2;
       //白棋
               if (n2==0||n2==7)
               label[n2]->setPixmap(QPixmap(":/pics/pic/WRook.png"));
               if (n2==1||n2==6)
               label[n2]->setPixmap(QPixmap(":/pics/pic/WKnight.png"));
               if (n2==2||n2==5)
               label[n2]->setPixmap(QPixmap(":/pics/pic/WBishop.png"));
               if (n2==3)
               label[n2]->setPixmap(QPixmap(":/pics/pic/WQueen.png"));
               if (n2==4)
               label[n2]->setPixmap(QPixmap(":/pics/pic/WKing.png"));
               if (n2<=15&&n2>=8)
               label[n2]->setPixmap(QPixmap(":/pics/pic/WPawn.png"));
           //黑棋
               if (n2==16||n2==23)
               label[n2]->setPixmap(QPixmap(":/pics/pic/BRook.png"));
               if (n2==17||n2==22)
               label[n2]->setPixmap(QPixmap(":/pics/pic/BKnight.png"));
               if (n2==18||n2==21)
               label[n2]->setPixmap(QPixmap(":/pics/pic/BBishop.png"));
               if (n2==19)
               label[n2]->setPixmap(QPixmap(":/pics/pic/BQueen.png"));
               if (n2==20)
               label[n2]->setPixmap(QPixmap(":/pics/pic/BKing.png"));
               if (n2<=31&&n2>=24)
               label[n2]->setPixmap(QPixmap(":/pics/pic/BPawn.png"));

       label[n2]->move((x2)*height()/29*3+height()/29*2.8,(y2)*height()/29*3+height()/29*2.4);
       if (myChess[n2].white)
           white++;
       else
           black++;
    }
    int xk=x2-x1;
    myChess[n1].moved--;
    myChess[n1].x=x1;
    myChess[n1].y=y1;
    label[n1]->move((x1)*height()/29*3+height()/29*2.8,(y1)*height()/29*3+height()/29*2.4);
    //易位
    if (myChess[n1].chessType==4&&abs(xk)==2)
    {
        if (myChess[n1].white){
            if (xk>0){
                myChess[7].x+=2;
                //qDebug() << "白短易位1" << endl;
                castlinggg=0;
                label[7]->move((myChess[7].x)*height()/29*3+height()/29*2.8,(myChess[7].y)*height()/29*3+height()/29*2.4);
                myChess[7].moved=0;
            }
            else{
                myChess[0].x-=3;
                //qDebug() << "白长易位" << endl;
                castlinggg=0;
                label[0]->move((myChess[0].x)*height()/29*3+height()/29*2.8,(myChess[0].y)*height()/29*3+height()/29*2.4);
                myChess[0].moved=0;
            }
        }
        else{
            if (xk>0){
                myChess[23].x+=2;
                //qDebug() << "黑短易位" << endl;
                castlinggg=0;
                label[23]->move((myChess[23].x)*height()/29*3+height()/29*2.8,(myChess[23].y)*height()/29*3+height()/29*2.4);
                myChess[23].moved=0;
            }
            else{
                myChess[16].x-=3;
                //qDebug() << "黑长易位" << endl;
                castlinggg=0;
                label[16]->move((myChess[16].x)*height()/29*3+height()/29*2.8,(myChess[16].y)*height()/29*3+height()/29*2.4);
                myChess[16].moved=0;
            }
        }
    }
    //兵的底线升变
    if (((8<=n1&&n1<16)||(24<=n1&&n1<32))&&(y2==0||y2==7)){
        //qDebug() << "兵升变" << endl;
            myChess[n1].chessType=stone::pawn;
            if (myChess[n1].white)
                label[n1]->setPixmap(QPixmap(":/pics/pic/WPawn.png"));
            else
                label[n1]->setPixmap(QPixmap(":/pics/pic/BPawn.png"));
        label[n1]->resize(height()/29*3,height()/29*3);
        label[n1]->setFixedSize(height()/29*2.5,height()/29*3);
        label[n1]->setScaledContents(true);
        label[n1]->setGeometry((myChess[n1].x)*height()/29*3+height()/29*2.8,(myChess[n1].y)*height()/29*3+height()/29*2.4,height()/29*3,height()/29*3);
    }
    update();

}

//移动棋子
void setting::moveChess(int n1,int n2,int xp, int yp)
{
    h[i].n1=n1;
    h[i].n2=n2;
    h[i].x1=myChess[n1].x;
    h[i].y1=myChess[n1].y;
    h[i].x2=xp;
    h[i].y2=yp;

    castlinggg=0;
    //吃子
    if (n2!=-1){
       myChess[n2].dead=true;
       myChess[n2].x=-1;
       myChess[n2].y=-1;
       label[n2]->clear();
       if (myChess[n2].white)
           white--;
       else
           black--;
    }
    int xk=myChess[n1].x-xp;
    myChess[n1].x=xp;
    myChess[n1].y=yp;
    label[n1]->move((xp)*height()/29*3+height()/29*2.8,(yp)*height()/29*3+height()/29*2.4);
    //易位
    if (myChess[n1].chessType==4&&yp==myChess[n1].y&&abs(xk)==2)
    {
        //qDebug() << "1" << endl;
        if (myChess[n1].white){
            //qDebug() << "2" << endl;
            if (xk<0){
                myChess[7].x-=2;
                //qDebug() << "白短易位" << endl;
                castlinggg=1;
                label[7]->move((myChess[7].x)*height()/29*3+height()/29*2.8,(myChess[7].y)*height()/29*3+height()/29*2.4);
                update();
            }
            else{
                myChess[0].x+=3;
                //qDebug() << "白长易位" << endl;
                castlinggg=2;
                label[0]->move((myChess[0].x)*height()/29*3+height()/29*2.8,(myChess[0].y)*height()/29*3+height()/29*2.4);
                update();
            }
        }
        else{
            if (xk<0){
                myChess[23].x-=2;
                //qDebug() << "黑短易位" << endl;
                castlinggg=1;
                label[23]->move((myChess[23].x)*height()/29*3+height()/29*2.8,(myChess[23].y)*height()/29*3+height()/29*2.4);
            }
            else{ //long castling
                myChess[16].x+=3;
                //qDebug() << "黑长易位" << endl;
                castlinggg=2;
                label[16]->move((myChess[16].x)*height()/29*3+height()/29*2.8,(myChess[16].y)*height()/29*3+height()/29*2.4);
            }
        }
    }


    //兵的底线升变
    if (myChess[n1].chessType==stone::pawn&&(yp==0||yp==7)){
        QMessageBox mess(QMessageBox::Information, tr("提示"), tr("兵已达到底线，可选择升变为："));
            QPushButton *buttonRook = (mess.addButton(tr("车"), QMessageBox::AcceptRole));
            QPushButton *buttonQueen = (mess.addButton(tr("后"), QMessageBox::YesRole));
            QPushButton *buttonBishop = (mess.addButton(tr("象"), QMessageBox::RejectRole));
            QPushButton *buttonKnight = (mess.addButton(tr("马"), QMessageBox::RejectRole));
        mess.exec();
        //qDebug() << "兵升变" << endl;
        effect->setSource(QUrl::fromLocalFile(":/pics/sound/pawn.wav"));
        label[n1]->clear();
        if (mess.clickedButton()==buttonRook){
            myChess[n1].chessType=stone::rook;
            if (myChess[n1].white)
                label[n1]->setPixmap(QPixmap(":/pics/pic/WRook.png"));
            else
                label[n1]->setPixmap(QPixmap(":/pics/pic/BRook.png"));
        }
        else if(mess.clickedButton()==buttonQueen){
            myChess[n1].chessType=stone::queen;
            if (myChess[n1].white)
                label[n1]->setPixmap(QPixmap(":/pics/pic/WQueen.png"));
            else
                label[n1]->setPixmap(QPixmap(":/pics/pic/BQueen.png"));
        }
        else if(mess.clickedButton()==buttonBishop){
            myChess[n1].chessType=stone::bishop;
            if (myChess[n1].white)
                label[n1]->setPixmap(QPixmap(":/pics/pic/WBishop.png"));
            else
                label[n1]->setPixmap(QPixmap(":/pics/pic/BBishop.png"));
        }
        else if(mess.clickedButton()==buttonKnight){
            myChess[n1].chessType=stone::knight;
            if (myChess[n1].white)
                label[n1]->setPixmap(QPixmap(":/pics/pic/WKnigt.png"));
            else
                label[n1]->setPixmap(QPixmap(":/pics/pic/BKnigt.png"));
        }
        label[n1]->resize(height()/29*3,height()/29*3);
        label[n1]->setFixedSize(height()/29*2.5,height()/29*3);
        label[n1]->setScaledContents(true);
        label[n1]->setGeometry((myChess[n1].x)*height()/29*3+height()/29*2.8,(myChess[n1].y)*height()/29*3+height()/29*2.4,height()/29*3,height()/29*3);
    }
    update();
}
//最近的一步走动，用于记录
int setting::lastmove(int n)
{
    return n;
}

int setting::sumvalue(bool q)
{
    int line,i;
        line=0;
    int sum=0;
    for(i=31;i>15;i--)
    {
        if(!myChess[i].dead)
        {
            //棋子分值
            if(i!=20)
            {   sum+=myChess[i].value()+myChess[i].value()*0.01*(abs(myChess[i].y-line)*(4-abs(myChess[i].x-3.5)));}
            //王的分值
            else
            {
                sum+=(myChess[i].value());
            }
        }
    }
    line=7;
    for(i=0;i<16;i++)
    {
        if(!myChess[i].dead)
        {
            //棋子分值
            if(i!=4)
            {   sum-=myChess[i].value()+myChess[i].value()*0.01*(abs(myChess[i].y-line)*(4-abs(myChess[i].x-3.5)));}
            //王的分值
            else
            {
                sum-=(myChess[i].value());
            }
        }
    }
    if(q)
        sum=-sum;
    return sum;
}


void setting::mouseMoveEvent(QMouseEvent *e){
    QToolTip::showText(e->globalPos(), QString("( %1, %2)").arg(e->x()).arg(e->y()));
    update();
}

QMessageBox::StandardButton result;
void setting::mousePressEvent(QMouseEvent *e)
{
    static int move_flag=0,n1=0,n2=0;
    int xp=-1,yp=-1;
    //获取点击坐标
    for (int i=0;i<8;i++){
        for (int j=0;j<9;j++){
            if (pow(e->x()-(i+1)*height()/29*3-height()/29*2.8,2)+pow(e->y()-(j)*height()/29*3-height()/29*2.4,2)<pow(height()/29*3,2)){
                xp=i;
                yp=j;
                break;
            }
        }
    }
    if (xp>-1&&yp>-1){
        switch (move_flag) {
        case 0:{
            n1=getNum(xp,yp);
            move_flag++;
        }
            break;
        case 1:{
            n2=getNum(xp,yp);
            move_flag++;
        }
            break;
        }
        if (move_flag==2)
        {
            move_flag=0;
            if (move(n1,n2,xp,yp)) {
                nn=lastmove(n1);
                effect->setSource(QUrl::fromLocalFile(":/pics/sound/movechess.wav"));
                moveChess(n1,n2,xp,yp);

                i++;
                myChess[n1].moved++;

                QFont font ( 0, 20, 75); //第一个属性是字体（微软雅黑），第二个是大小，第三个是加粗（权重是75）
                ui->label1->move(890,100);
                if(chessmode==1)
                    whiteTurn*=-1;
                if(whiteTurn==1)
                    ui->label1->setText("白棋执子");
                else
                    ui->label1->setText("黑棋执子");
                ui-> label1->setFont(font);
                ui->label2->move(890,300);
                if(chessmode==1)
                    whiteTurn*=-1;
                char a[10];
                int ii=0;
                //易位记录
                if(castlinggg!=0)
                {
                    if(castlinggg==1)
                    {a[ii++]='o';a[ii++]='-';a[ii++]='o';a[ii++]='\0';}
                    else
                    {a[ii++]='o';a[ii++]='-';a[ii++]='o';a[ii++]='-';a[ii++]='o';a[ii++]='\0';}
                }
                //普通走棋记录
                else
                {
                    a[ii]=myChess[n1].getsignal();
                    if(n2!=-1)
                    {
                        a[++ii]='x';
                    }
                    a[++ii]=xp+'a';
                    a[++ii]=8-yp+'0';
                    a[++ii]='\0';
                }
                ui->label2->setText(a);
                ui-> label2->setFont(font);
                whiteTurn*=-1;
                if (win()||bihe()||heqi())
                {
                    //qDebug() << white<<"棋子 "<< endl;
                    effect->setSource(QUrl::fromLocalFile(":/pics/sound/victory.wav"));
                    if (win())
                    {
                        if (whiteTurn==1)
                            result=QMessageBox::information(NULL,"祝贺！","黑方获胜",QMessageBox::Retry|QMessageBox::Ok);
                        else
                            result=QMessageBox::information(NULL,"祝贺！","白方获胜",QMessageBox::Retry|QMessageBox::Ok);
                    }
                    else if (bihe()){
                        result=QMessageBox::information(NULL,"提示！","逼和!",QMessageBox::Retry|QMessageBox::Ok);
                    }
                    else if (heqi()){
                        result=QMessageBox::information(NULL,"提示！","和棋!",QMessageBox::Retry|QMessageBox::Ok);
                    }
                    if (result==QMessageBox::Ok){
                        this->close();
                        QApplication::exit();
                    }
                    else{
                        QApplication::quit();
                        QProcess::startDetached(QApplication::applicationFilePath(),QStringList());
                    }
                }
                whiteTurn*=-1;

                if(chessmode==2)
                {

                    t=3;
                    for(int i=0;i<4;i++)
                    {best[i].s(-1,-1,-1,-1,-100000);}
                    d=aiMove();
                    whiteTurn*=-1;

                    QTimer::singleShot(1000,this,SLOT(timechess()));
                }
                whiteTurn*=-1;
            }
            else if (isthreatened){
                effect->setSource(QUrl::fromLocalFile(":/pics/sound/warning.wav"));
                QMessageBox::information(NULL,"警告","王正在被将军",QMessageBox::Retry|QMessageBox::Ok,QMessageBox::Retry);
            }
            else{
                effect->setSource(QUrl::fromLocalFile(":/pics/sound/warning.wav"));
                QMessageBox::information(NULL,"警告","非法移动，请重试！",QMessageBox::Retry|QMessageBox::Ok,QMessageBox::Retry);
            }

        }
    }
}
void setting::timechess()
{
    moveChess(d.n1,d.n2,d.x1,d.y1);
    effect->setSource(QUrl::fromLocalFile(":/pics/sound/movechess.wav"));
}
//悔棋
void setting::on_pushButton_clicked()
{
    if(i>0)
    {i--;
    hui(h[i].n1,h[i].n2,h[i].x1,h[i].x2,h[i].y1,h[i].y2);}
}
//双人对战
void setting::on_pushButton_2_clicked()
{
    chessmode=1;
}
//人机对战
void setting::on_pushButton_3_clicked()
{
    chessmode=2;
}

//连接网页
void setting::on_pushButton_4_clicked()
{
    QDesktopServices::openUrl(QUrl("https://lichess.org/"));
}
